package com.kaifamiao.schedule;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;

public class JoinTest2 {

    public static Thread produce(String name, Thread another) {

        Callable<?> callable = () -> {
            Thread x = Thread.currentThread();
            for (int i = 1; i < 6; i++) {
                System.out.printf("%s 线程 - %d .\n", x.getName() , i);
                if( another != null && i == 2 ) {
                    // 等待另一个线程执行结束
                    another.join();
                }
            }
            return null;
        };

        RunnableFuture<?> task = new FutureTask<>( callable );

        return new Thread( task, name );
    }

    public static void main(String[] args){

        Thread second = produce( "张骞" , null );
        Thread first = produce( "刘彻" , second );

        first.start();
        second.start();

    }

}
